﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    public sealed class ConfigurationController : ExtController
    {
        #region GET/POST Actions

        [HttpPost]
        public ActionResult Locales()
        {
            var m = Svc.ProcessOperation("GetLocalizationProfileDictionary", new
            {
                FlagMask = "Active,Public"
            });
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new
                {
                    id = JDM.String(i.Key),
                    value = JDM.String(i.Value)
                });
            }
            return Json(data.ToArray());
        }

        [HttpPost]
        public ActionResult Languages()
        {
            var m = Svc.ProcessOperation("GetLanguageDictionary");
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new
                {
                    id = JDM.String(i.Key),
                    value = JDM.String(i.Value)
                });
            }
            return Json(data.ToArray());
        }

        [HttpPost]
        public ActionResult Countries()
        {
            var m = Svc.ProcessOperation("GetCountryDictionary");
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new
                {
                    id = JDM.String(i.Key),
                    value = JDM.String(i.Value)
                });
            }
            return Json(data.ToArray());
        }

        [HttpPost]
        public ActionResult Regions(int countryID)
        {
            var m = Svc.ProcessOperation("GetRegionDictionary", new
            {
                CountryID = countryID
            });
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new
                {
                    id = JDM.String(i.Key),
                    value = JDM.String(i.Value)
                });
            }
            return Json(data.ToArray());
        }

        [HttpPost]
        public ActionResult LookupAttributes(string term, string flagMask)
        {
            var m = Svc.ProcessOperation("GetLookupAttributeDictionary", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                FlagMask = flagMask,
                Term = new
                {
                    Expression = String.Format("%{0}%", term)
                },
                Count = 10
            });
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new
                {
                    id = JDM.String(i.Key),
                    value = JDM.String(i.Value)
                });
            }
            return Json(data.ToArray());
        }

        #endregion

        #region Child Actions

        [ChildActionOnly, ActionCache("long", true, VaryByCustom = "locale")]
        public ActionResult _Locales()
        {
            var m = Svc.ProcessOperation("SearchLocalizationProfiles", new
            {
                FlagMask = "Active,Public",
                SortBy = new[] 
                {
                    "Code"
                }
            }, new
            {
                DisclosureLevel = "Base"
            });
            return PartialView(m);
        }

        #endregion
    }
}
